package com.example.day04;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day04.net.ViewMian;
import com.example.day04.present.Present;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewMian, View.OnClickListener {

    private Toolbar mToob;
    private RecyclerView mRecy;
    private ArrayList<Bean.RecentBean> beans;
    private ArrayList<Boolean> boo;
    private Recy_Adapter adapter;
    private Button mBianji;
    private Button mQx;
    private Button mQuren;
    int ip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Present present = new Present(this);
        present.getData();
        initView();
    }

    private void initView() {
        mToob = (Toolbar) findViewById(R.id.toob);
        mRecy = (RecyclerView) findViewById(R.id.recy);
        mBianji = (Button) findViewById(R.id.bianji);
        mBianji.setOnClickListener(this);
        mQx = (Button) findViewById(R.id.qx);
        mQx.setOnClickListener(this);
        mQuren = (Button) findViewById(R.id.quren);
        mQuren.setOnClickListener(this);
        mToob.setTitle("");
        setSupportActionBar(mToob);


        mRecy.setLayoutManager(new LinearLayoutManager(this));
        mRecy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        beans = new ArrayList<>();
        boo = new ArrayList<>();
        adapter = new Recy_Adapter(this, beans);
        adapter.setData(beans,boo);
        mRecy.setAdapter(adapter);
        adapter.setOnItemClic(new Recy_Adapter.OnItemClic() {
            @Override
            public void getbean(int i) {
                ip=i;
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                intent.putExtra("name",beans.get(i).getUrl());
                startActivityForResult(intent,100);
            }
        });



    }

    @Override
    public void showData(List<Bean.RecentBean> recentBeanList) {
        beans.addAll(recentBeanList);
        adapter.notifyDataSetChanged();



    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this, "str", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bianji:
                adapter.br=true;
                mQx.setVisibility(View.VISIBLE);
                mBianji.setVisibility(View.INVISIBLE);
                mQuren.setVisibility(View.VISIBLE);
                adapter.notifyDataSetChanged();
                break;
            case R.id.qx:
                break;
            case R.id.quren:
                DeleteItem();
                break;
            default:

                break;
        }
    }
    public void DeleteItem(){
        beans.remove(ip);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100&&requestCode==RESULT_OK){
            long time = data.getLongExtra("time", 0);
            adapter.notifyDataSetChanged();
        }
    }
}
